//
//  FWEssenceTypeController.m
//  BirdLoveSheep
//
//  Created by wwj on 16/3/22.
//  Copyright © 2016年 wwj. All rights reserved.
//

#import "FWEssenceTypeController.h"
#import "FWEssenceService.h"
#import "FWEssenceCell.h"
#import "FWTopicFrameModel.h"
#import "FWCommentController.h"
#import <MJRefresh.h>

@interface FWEssenceTypeController ()
@property (strong, nonatomic) NSMutableArray *dataArray;
@property (strong, nonatomic) FWEssenceRequest *request;
@property (assign, nonatomic) NSInteger selectIndex;
@end

@implementation FWEssenceTypeController
static NSString *const ID = @"FWEssenceCell";

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)setEssenceType:(NSInteger)essenceType{
    _essenceType = essenceType;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.contentInset = UIEdgeInsetsMake(104, 0, 48, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:ID bundle:nil] forCellReuseIdentifier:ID];
    self.tableView.backgroundColor = [UIColor clearColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onClickRefrsh) name:FWTabBarDidSelectNotification object:nil];
    [self addRefresh];
    
}

- (void)onClickRefrsh{
    if (self.tabBarController.selectedIndex == self.selectIndex &&
        [self.view isShowingOnKeyWindow]) {
        [self.tableView.mj_header beginRefreshing];
    }
    self.selectIndex = self.tabBarController.selectedIndex;
    
}

- (void)addRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNewData)];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(lodMoreData)];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    self.tableView.mj_footer.hidden = (self.dataArray.count == 0);
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FWEssenceCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    cell.modelF = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    FWCommentController *commentVC = [[FWCommentController alloc] init];
    commentVC.modelF = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:commentVC animated:YES];
}

- (void)getNewData{
    FWEssenceRequest *request = [[FWEssenceRequest alloc] init];
    NSNumber *lastPage = self.request.page;
    request.a = @"list";
    request.c = @"data";
    request.type = self.essenceType;
    request.page = @1;
    self.request = request;
    [FWEssenceService getEssenceDataWith:self.request success:^(FWEssenceResponse *response) {
        [self.tableView.mj_header endRefreshing];
        if (request != self.request) return;
        
        self.request.maxtime = response.maxtime;
        
        [self.dataArray removeAllObjects];
        
        for (FWTopicModel *model in response.list) {
            FWTopicFrameModel *frameModel = [[FWTopicFrameModel alloc] init];
            frameModel.topic = model;
            [self.dataArray addObject:frameModel];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        self.request.page = lastPage;
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)lodMoreData{
    FWEssenceRequest *request = [[FWEssenceRequest alloc] init];
    NSNumber *lastPage = self.request.page;
    request.a = @"list";
    request.c = @"data";
    request.type = self.essenceType;
    request.page = @(self.request.page.intValue +1);
    request.maxtime = self.request.maxtime;
    self.request = request;
    [FWEssenceService getEssenceDataWith:self.request success:^(FWEssenceResponse *response) {
        [self.tableView.mj_footer endRefreshing];
        if (self.request != request) return;
        self.request.maxtime = response.maxtime;
    
        NSMutableArray *array = [NSMutableArray array];
        for (FWTopicModel *model in response.list) {
            FWTopicFrameModel *frameModel = [[FWTopicFrameModel alloc] init];
            frameModel.topic = model;
            [array addObject:frameModel];
        }
        [self.dataArray addObjectsFromArray:array];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        self.request.page = lastPage;
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    FWTopicFrameModel *frameModel = self.dataArray[indexPath.row];
    return frameModel.cellHeight;
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
